import os
import time


class FieldException(Exception):
    pass


class ObjectException(Exception):
    pass


global border


def clc():
    os.system("clear")


def clcw():
    os.system("cls")


class Canvas:

    def __init__(self, width, height):
        self.lst = []
        self.objs = []
        self.width = width
        self.height = height
        self.lst = ["."] * width
        for i in range(self.width):
            self.lst[i] = ["."] * self.height

    def printcvs(self):
        for i in range(len(self.lst)):
            print(*self.lst[i])

    def frame(self, delay=1.0, system="win"):
        if system == "win":
            clcw()
            self.printcvs()
        elif system == "lin" or system == "mac":
            clc()
            self.printcvs()
        time.sleep(delay)
# Work In Progress!
    def is_border(self, coords):
        ret = True
        try:
            self.lst[coords[0]][coords[1]] == "."
        except:
            ret = False
        return ret


class Object:
    def __init__(self, lst, sym, lx, ly, layer=1):
        self.lst = lst
        self.sym = sym
        idx = -1
        try:
            idx = lst.objs.index(self)
        except:
            pass
        if idx == -1:
            if sym != ".":
                lst.lst[ly][lx] = sym
                lst.objs.append(self)
                lst.objs.append(lx)
                lst.objs.append(ly)
                lst.objs.append(sym)
                lst.objs.append(layer)
            else:
                raise FieldException("You cannot create an empty space. Note: '.' means empty space")
        else:
            raise ObjectException(f"Name '{self}' is already taken")

    def erase(self):
        idx = -1
        try:
            idx = self.lst.objs.index(self)
        except:
            pass
        if idx != -1:
            self.lst.lst[self.lst.objs[idx + 2]][self.lst.objs[idx + 1]] = "."
            del (self.lst.objs[idx], self.lst.objs[idx + 1])
            del (self.lst.objs[idx])
            del (self.lst.objs[idx])
            del (self.lst.objs[idx])
        else:
            raise ObjectException(f"Object '{self}' not found")

    def move(self, movex, movey):
        idx = -1
        try:
            idx = self.lst.objs.index(self)
        except:
            pass
        if idx != -1:
            if movex != 0 or movey != 0:
                sym = self.lst.lst[self.lst.objs[idx + 2]][self.lst.objs[idx + 1]]
                self.lst.lst[self.lst.objs[idx + 2] + movey][self.lst.objs[idx + 1] + movex] = sym
                self.lst.lst[self.lst.objs[idx + 2]][self.lst.objs[idx + 1]] = "."
                self.lst.objs[idx + 1] += movex
                self.lst.objs[idx + 2] += movey
        else:
            raise ObjectException(f"Object '{self}' not found")

    def coords(self):
        idx = -1
        try:
            idx = self.lst.objs.index(self)
        except:
            pass
        if idx != -1:
            return [self.lst.objs[idx + 1], self.lst.objs[idx + 2]]

    def find(self):
        idx = -1
        try:
            idx = self.lst.objs.index(self)
        except:
            pass
        if idx == -1:
            idx = 0
        else:
            idx = 1
        return idx

# Work In Progress!
class Group:
    def __init__(self, *args):
        self.list = []
        for x in args:
            self.list.append()
